{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<TabPageHeader @model={{@backendModel}} @breadcrumbs={{@breadcrumbs}}>
  <:toolbarFilters>
    {{#if (and (not @promptConfig) @roles.meta.total)}}
      <FilterInput placeholder="Filter roles" value={{@pageFilter}} @autofocus={{true}} @onInput={{this.onFilterChange}} />
    {{/if}}
  </:toolbarFilters>
  <:toolbarActions>
    {{#if @promptConfig}}
      <ToolbarLink @route="configure" data-test-toolbar-action="config">
        Configure LDAP
      </ToolbarLink>
    {{else}}
      <ToolbarLink @route="roles.create" @type="add" data-test-toolbar-action="role">
        Create role
      </ToolbarLink>
    {{/if}}
  </:toolbarActions>
</TabPageHeader>

{{#if @promptConfig}}
  <ConfigCta />
{{else if (not @roles.meta.filteredTotal)}}
  {{#if @pageFilter}}
    <EmptyState @title="There are no roles matching &quot;{{@pageFilter}}&quot;" />
  {{else}}
    <EmptyState
      data-test-config-cta
      @title="No roles created yet"
      @message="Roles in Vault will allow you to manage LDAP credentials. Create a role to get started."
    >
      <LinkTo class="has-top-margin-xs" @route="roles.create">
        Create role
      </LinkTo>
    </EmptyState>
  {{/if}}
{{else}}
  <div class="has-bottom-margin-s">
    {{#each @roles as |role|}}
      <ListItem @linkPrefix={{this.mountPoint}} @linkParams={{array "roles.role.details" role.type role.name}} as |Item|>
        <Item.content>
          <Icon @name="user" />
          <span data-test-role={{role.name}}>{{role.name}}</span>
          <Hds::Badge @text={{role.type}} data-test-role-type-badge={{role.name}} />
        </Item.content>
        <Item.menu>
          {{#if role.rolePath.isLoading}}
            <li class="action">
              <Hds::Button disabled @color="tertiary" @icon="loading" @text="loading" @isIconOnly={{true}} />
            </li>
          {{else}}
            <li class="action">
              <LinkTo
                class="has-text-black has-text-weight-semibold"
                data-test-edit
                @route="roles.role.edit"
                @models={{array role.type role.name}}
                @disabled={{not role.canEdit}}
              >
                Edit
              </LinkTo>
            </li>
            <li class="action">
              <LinkTo
                class="has-text-black has-text-weight-semibold"
                data-test-get-creds
                @route="roles.role.credentials"
                @models={{array role.type role.name}}
                @disabled={{not role.canReadCreds}}
              >
                Get credentials
              </LinkTo>
            </li>
            {{#if role.canRotateStaticCreds}}
              <ConfirmAction
                data-test-rotate-creds
                @isInDropdown={{true}}
                @id={{concat "rotate-" role.id}}
                @buttonText="Rotate credentials"
                @confirmMessage="When manually rotating credentials, the rotation period will start over."
                @modalColor="warning"
                @onConfirmAction={{fn this.onRotate role}}
              />
            {{/if}}
            <li class="action">
              <LinkTo
                class="has-text-black has-text-weight-semibold"
                data-test-details
                @route="roles.role.details"
                {{! this will force the roles.role model hook to fire since we may only have a partial model loaded in the list view }}
                @models={{array role.type role.name}}
                @disabled={{not role.canRead}}
              >
                Details
              </LinkTo>
            </li>
            {{#if role.canDelete}}
              <ConfirmAction
                data-test-delete
                @isInDropdown={{true}}
                @buttonText="Delete"
                @confirmMessage="Deleting this role means that you’ll need to recreate it in order to generate credentials again."
                @onConfirmAction={{fn this.onDelete role}}
              />
            {{/if}}
          {{/if}}
        </Item.menu>
      </ListItem>
    {{/each}}

    <Hds::Pagination::Numbered
      @currentPage={{@roles.meta.currentPage}}
      @currentPageSize={{@roles.meta.pageSize}}
      @route="roles"
      @showSizeSelector={{false}}
      @totalItems={{@roles.meta.filteredTotal}}
      @queryFunction={{this.paginationQueryParams}}
      data-test-pagination
    />
  </div>
{{/if}}